package com.example.agriculyural.FactoryMethod;
// 农机设备抽象类

import com.example.agriculyural.Observer.EnvironmentData;
import com.example.agriculyural.Observer.EnvironmentObserver;
import com.example.agriculyural.Strategy.AvoidWaterStrategy;
import com.example.agriculyural.Strategy.BadWeatherStrategy;
import com.example.agriculyural.Strategy.NormalPathStrategy;
import com.example.agriculyural.Strategy.PathPlanningStrategy;

public abstract class AgriculturalMachine implements EnvironmentObserver {
    protected String id;
    protected String status;
    protected PathPlanningStrategy pathStrategy;

    public AgriculturalMachine(String id) {
        this.id = id;
        this.status = "待机";
        this.pathStrategy = new NormalPathStrategy();
    }

    public abstract void performOperation();
    public abstract String getMachineType();

    public void setPathStrategy(PathPlanningStrategy strategy) {
        this.pathStrategy = strategy;
    }

    @Override
    public void update(EnvironmentData data) {
        System.out.println(getMachineType() + " " + id + " 接收到环境数据更新");

        // 根据环境数据选择策略
        if (data.getSoilMoisture() > 80) {
            setPathStrategy(new AvoidWaterStrategy());
        } else if (data.getWindSpeed() > 15) {
            setPathStrategy(new BadWeatherStrategy());
        } else {
            setPathStrategy(new NormalPathStrategy());
        }

        pathStrategy.planPath(data);
        performOperation();
    }

    public String getId() { return id; }
    public String getStatus() { return status; }
}
